package cn.hh.user.controller;

import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.user.domain.User;
import cn.hh.user.query.UserQuery;
import cn.hh.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /*编辑*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if(user.getId()==null){
                userService.add(user);
            }else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败"+e.getMessage());
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            userService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            userService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("批量删除失败"+e.getMessage());
        }
    }
    /*分页查询*/
    @PostMapping
    public PageList<User> querPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

}
